package wallet

import (
	"gitee.com/xuxinzhi2206/corehub/database/mysql"
	"gorm.io/gorm"
)

// UserConsumptionDetails 用户消费明细表
type UserConsumptionDetails struct {
	gorm.Model
	UserID          int64   `json:"userid" gorm:"column:userid;type:int(11);comment:用户ID"`
	TransactionType string  `json:"transaction_type" gorm:"column:transaction_type;type:varchar(20);comment:交易类型: recharge/consume/refund"`
	Amount          float64 `json:"amount" gorm:"column:amount;type:decimal(10, 2);comment:交易金额"`
	Balance         float64 `json:"balance" gorm:"column:balance;type:decimal(10, 2);comment:交易后的余额"`
	Description     string  `json:"description" gorm:"column:description;type:varchar(255);comment:交易描述"`
}

func (r *UserConsumptionDetails) CreatConsumption() (*UserConsumptionDetails, error) {
	return r, mysql.WithMysql(func(db *gorm.DB) error {
		return db.Create(r).Error
	})
}

func SearchConsumption(offset,limit  int) ([]*UserConsumptionDetails, int64, error) {
	var detail []*UserConsumptionDetails
	var total int64

	err := mysql.WithMysql(func(db *gorm.DB) error {
		off := (offset - 1) * limit
		if err := db.Offset(off).Limit(limit).Find(&detail).Error; err != nil {
			return err
		}

		if err := db.Model(&detail).Count(&total).Error; err != nil {
			return err
		}
		return nil
	})
	return detail, total, err
}
